package com.example.mybatisplus.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.common.utls.SessionUtils;
import com.example.mybatisplus.model.domain.Admin;
import com.example.mybatisplus.model.dto.DeleteDTO;
import com.example.mybatisplus.model.dto.PageDTO;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.WhitelistSettingService;
import com.example.mybatisplus.model.domain.WhitelistSetting;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


/**
 *
 *  前端控制器
 *
 *
 * @author fmx
 * @since 2024-07-14
 * @version v1.0
 */
@RestController
@RequestMapping("/api/whitelistSetting")
public class WhitelistSettingController {

    @Autowired
    private WhitelistSettingService whitelistSettingService;

    @PostMapping("login")
    // @ResponseBody
    public JsonResponse login(@RequestBody WhitelistSetting whitelistSetting){
        WhitelistSetting user = whitelistSettingService.login(whitelistSetting);
        return JsonResponse.success(user);
    }

    @GetMapping("logout")
    // @ResponseBody
    public JsonResponse logout(){
        SessionUtils.clearCurUser();
        return JsonResponse.success(true);
    }

    @GetMapping("pageList")
    // @ResponseBody
    public JsonResponse pageList(WhitelistSetting whitelistSetting, PageDTO dto){
        Page<WhitelistSetting> page = whitelistSettingService.pageList(whitelistSetting, dto);
        return JsonResponse.success(page);
    }

    // 删除get
    @GetMapping("removeById")
    public JsonResponse removeById(Long id){
        // 返回值表示是否删除成功
        boolean b = whitelistSettingService.removeById(id);
        // res.data
        return JsonResponse.success(b);
    }

    // 删除post
    @PostMapping("removeById2")
    public JsonResponse removeById2(@RequestBody WhitelistSetting whitelistSetting){
        // 返回值表示是否删除成功
        boolean b = whitelistSettingService.removeById(whitelistSetting.getId());
        // res.data
        return JsonResponse.success(b);
    }

    // 批量删除
    @PostMapping("removeByIds")
    public JsonResponse removeByIds(@RequestBody DeleteDTO dto){
        // 返回值表示是否删除成功
        boolean b = whitelistSettingService.removeByIds(dto.getIds());
        // res.data
        return JsonResponse.success(b);
    }

    // 修改用户
    @PostMapping("edit")
    public JsonResponse edit(@RequestBody WhitelistSetting whitelistSetting){
        // 返回值表示是否删除成功
        boolean b = whitelistSettingService.updateById(whitelistSetting);
        // res.data
        return JsonResponse.success(b);
    }

    // 数据导出
    // 必须post请求 参数中必须有响应 不需要返回
    @PostMapping("exportUser")
    public void exportUser(HttpServletResponse response){
        whitelistSettingService.exportUser(response);
    }

    // 文件导入
    @RequestMapping(value = "/importUser")
    public JsonResponse upload(MultipartFile file) throws IOException {
        whitelistSettingService.importUser(file);
        return JsonResponse.success(true);
    }

}

